package fei.yuzhi.com.loveflowers.fragment.firstpagefragment;

import android.content.Intent;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import fei.yuzhi.com.loveflowers.bean.firstpagefrgamentbean.AdvListBean;
import fei.yuzhi.com.loveflowers.bean.firstpagefrgamentbean.Home1Bean;
import fei.yuzhi.com.loveflowers.bean.firstpagefrgamentbean.Home3Bean;
import fei.yuzhi.com.loveflowers.bean.firstpagefrgamentbean.Home7Bean;
import fei.yuzhi.com.loveflowers.utils.FirstPageHttpUtils;
import fei.yuzhi.com.loveflowers.utils.JieXi;

/**
 * Created by My on 2016/10/5.
 */
public class FirstPageModle implements FirstPageContract.Model {
    private String mFirstImageUrl;//轮播图下方图片的图片地址
    private String mTimeLimtImageUrl;//限时秒杀图片的地址
    private String mTodayImageUrl;//今日推荐图片的地址
    private String mHotSaleImageUrl;//爱金花热销图片的地址
    private List<String> mAutoScrollUrls =new ArrayList<>();//自动轮播图片的地址数组
    private List<String> mFirstRvImageUrl=new ArrayList<>();//首页中第一个出现的gridview
    private List<String> mSecondRvImageUrl=new ArrayList<>();//限时秒杀下的gridview
    private List<String> mThreadRvImageUrl =new ArrayList<>();//今日推荐下的gridview
    private List<String> mHorizontalRvImageUrl=new ArrayList<>();//爱金花热销下的listview
    private List<String> mFirstRvData=new ArrayList<>();//限时秒杀下gridview的图片id集合
    private List<String> mSecondRvData=new ArrayList<>();//今日推荐下的gridview的图片id集合
    private List<String> mAutoScrollData=new ArrayList<>();//自动轮播图的id集合
    private List<String> mListViewData=new ArrayList<>();//底部listview的id集合
    //将所需图片在集合中的下标定义成常量
    static final int mFirstImageNum =0;
    static final int mTimeLimtImageNum =1;
    static final int mTodayImageNum =2;
    static final int mHotSaleImageNum =3;
    @Override
    public void getImage(final onGetImageUrlListener getImageUrlListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FirstPageHttpUtils.getMapFromNet(new FirstPageHttpUtils.onGetMapByNet() {
                    @Override
                    public void getMap(Map<String, List<?>> map) {
                        //通过解析json数据，得到所需要的数据集合
                        List<Home1Bean> home1BeanList= (List<Home1Bean>) map.get(JieXi.HOME1BEANLIST);
                        for (int i=0;i<home1BeanList.size();i++){
                            //根据定义好的常量下标得到所需要的图片地址
                            mFirstImageUrl =home1BeanList.get(mFirstImageNum).getImage();
                            mTimeLimtImageUrl =home1BeanList.get(mTimeLimtImageNum).getImage();
                            mTodayImageUrl =home1BeanList.get(mTodayImageNum).getImage();
                            mHotSaleImageUrl =home1BeanList.get(mHotSaleImageNum).getImage();
                        }
                        //得到底部listview所需要的图片地址，因集合钱四个数据为其它图片，所以i从5开始
                        for (int i=mHotSaleImageNum+1;i<home1BeanList.size();i++){
                            mHorizontalRvImageUrl.add(home1BeanList.get(i).getImage());
                            mListViewData.add(home1BeanList.get(i).getData());
                        }
                        //通过定义好的接口将得到的图片地址以及图片地址的集合传递出去
                        getImageUrlListener.loadImageUrl(mFirstImageUrl, mTimeLimtImageUrl,
                                mTodayImageUrl, mHotSaleImageUrl,mHorizontalRvImageUrl,mListViewData);
                    }
                });
            }
        }).start();
    }

    @Override
    public void getAutoScrollIamgeUrl(final onGetAutoScrollImageUrlListener
                                                  getAutoScrollImageUrlListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FirstPageHttpUtils.getMapFromNet(new FirstPageHttpUtils.onGetMapByNet() {
                    @Override
                    public void getMap(Map<String, List<?>> map) {
                        //得到所需要的bean集合
                        List<AdvListBean> advListBeanList= (List<AdvListBean>) map.get(JieXi.ADVLISTBEEN);
                        for (int i=0;i<advListBeanList.size();i++){
                            //通过bean集合得到轮播图片地址的集合
                            mAutoScrollUrls.add(advListBeanList.get(i).getImage());
                            mAutoScrollData.add(advListBeanList.get(i).getData());
                            //通过接口传递轮播图片的地址集合
                            getAutoScrollImageUrlListener.loadImageUrlList(mAutoScrollUrls,mAutoScrollData);
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    public void getGridImage(final onGetGirdRecyclerViewImageListener
                                              getFirstRecyclerViewImageListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FirstPageHttpUtils.getMapFromNet(new FirstPageHttpUtils.onGetMapByNet() {
                    @Override
                    public void getMap(Map<String, List<?>> map) {
                        //得到第一个gridview所需要的bean集合
                        List<Home7Bean> home7BeanList= (List<Home7Bean>) map.get(JieXi.HOME7BEEN);
                        //因为其余的gridview所需要的数据在一个bean集合内，所以再得到此集合即可
                        List<Home3Bean> home3BeanList= (List<Home3Bean>) map.get(JieXi.HOME3BEEN);
                        for (int i=0;i<home7BeanList.size();i++){
                            //得到第一个gridview所需要的图片地址集合
                            mFirstRvImageUrl.add(home7BeanList.get(i).getImage());
                        }
                        for (int i=0;i<home3BeanList.size()/2;i++){
                            //得到限时秒杀下gridview所需要的图片地址的集合
                            mSecondRvImageUrl.add(home3BeanList.get(i).getImage());
                            mFirstRvData.add(home3BeanList.get(i).getData());
                        }
                        for (int i=home3BeanList.size()/2;i<home3BeanList.size();i++){
                            //得到今日推荐下gridview所需要的图片地址的集合
                            mThreadRvImageUrl.add(home3BeanList.get(i).getImage());
                            mSecondRvData.add(home3BeanList.get(i).getData());
                        }
                        //通过接口回调传递以上所获得的所有图片地址的集合
                        getFirstRecyclerViewImageListener.loadImages(mFirstRvImageUrl,
                                mSecondRvImageUrl, mThreadRvImageUrl,
                                mFirstRvData,mSecondRvData);
                    }
                });
            }
        }).start();
    }


    //定义接口，用来回调网络加载得到的图片地址，传递到presenter中
    public interface onGetImageUrlListener{
        void loadImageUrl(String firstImageUrl,String timeLimtImageUrl,
                          String todayImageUrl,String hotSaleImageUrl,
                          List<String> horizontalRVImageUrl,
                          List<String> mListViewData);
    }
    //定义接口，用来回调网络加载得到的滚动轮播图片的地址数组，传递到presenter中
    public interface onGetAutoScrollImageUrlListener{
        void loadImageUrlList(List<String> urls,List<String> mAutoScrollData);
    }
    //定义接口，用来回调网络加载得到的recyclerView所需要的图片
    public interface onGetGirdRecyclerViewImageListener {
        void loadImages(List<String> firstGirdRvImageUrl,
                        List<String> secondGirdRvImageUrl,
                        List<String> threadGirdRvImageUrl,
                        List<String> mFirstRvData,
                        List<String> mSecondRvData);
    }
}
